{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "from datetime import datetime\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib.lines import Line2D\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vmode_re = re.compile(r'(?P<datetime>\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})\\s+(?P<pthreadid>\\d+)\\s+(?P<pid>\\d+) V homebrain:.* \\(\\(virtualmode (?P<vmid>\\d+)\\)\\)$')\n",
    "\n",
    "dts = []\n",
    "ids = []\n",
    "\n",
    "def parser(file):\n",
    "    try:\n",
    "        with open(file) as f:\n",
    "            for line in f:\n",
    "                res = vmode_re.search(line)\n",
    "                if res is None:\n",
    "                    continue\n",
    "                gdict = res.groupdict()\n",
    "                dt = datetime.strptime(gdict['datetime'], '%m-%d %H:%M:%S.%f').replace(year=2018)\n",
    "                dts.append(dt)\n",
    "                ids.append(int(gdict['vmid']))\n",
    "\n",
    "    except IOError as e:\n",
    "        raise e\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parser('./logcat.log.01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# s = pd.Series(data=ids, index=dts)\n",
    "# s.plot(kind='bar', rot=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-2014bdcbe230>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0max\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_subplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m111\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m# ax.xaxis_date()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;31m# ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;31m# ax.plot(dts, ids, 'ro', markersize=12)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "# ax.xaxis_date()\n",
    "# ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))\n",
    "# ax.plot(dts, ids, 'ro', markersize=12)\n",
    "\n",
    "plt.bar(dts, ids, width=1/5.5)\n",
    "# plt.grid(True)\n",
    "plt.xlabel('DateTime')\n",
    "plt.ylabel('VirtualMode')\n",
    "plt.ylim(0, 12)\n",
    "# plt.axvline(x=dts[0], ymax=4)\n",
    "# ax.add_line(Line2D([1, 0], [2, ids[0]]))\n",
    "# ax.fill_between(x=dts, y1=ids)\n",
    "# ax.fill(dts, ids, 'b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = [3, 10, 7, 5, 3, 4.5, 6, 8.1]\n",
    "N = len(y)\n",
    "x = range(N)\n",
    "width = 1/1.5\n",
    "plt.bar(x, y, width, color=\"blue\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.subplot(111)\n",
    "# remove x,y axis mark\n",
    "# plt.xticks([])\n",
    "# plt.yticks([])\n",
    "# plt.axis('off')\n",
    "xdata = np.arange(0, len(dts))\n",
    "plt.plot(xdata, ids, 'ro')\n",
    "plt.xlim(-1, len(dts))\n",
    "plt.ylim(0, 12)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
